const shareUtil = require('../../../utils/shareUtil.js');
const app = getApp();
let imgUrl=app.globalData.imgUrl
var url = "https://erjc.aexcx.cn/app"

Page({
    data: {
        language: [ {
            name: "中文",
            code: "zh"
        }, {
            name: "英语",
            code: "en"
        }, {
            name: "日语",
            code: "jp"
        }, {
            name: "韩语",
            code: "kor"
        }, {
            name: "泰语",
            code: "th"
        }, {
            name: "法语",
            code: "fra"
        } ],
        languageList: [],
        fromLanguage: {
            name: "英语",
            code: "en"
        },
        toLanguage: {
            name: "中文",
            code: "zh"
        },
        query: "",
        result: "",
        voiceUrl: "",
        imgUrl :imgUrl
    },
    onLoad: function(a) {
        for (var e = this, t = [], n = e.data.language, o = 0; o < n.length; o++) t.push(n[o].name);
        e.setData({
            languageList: t
        }), 
        wx.getStorage({
            key: "fromLanguage",
            success: function(a) {
                e.setData({
                    fromLanguage: a.data
                });
            }
        }), 
        wx.getStorage({
            key: "toLanguage",
            success: function(a) {
                e.setData({
                    toLanguage: a.data
                });
            }
        });
    },
    getInput: function(a) {
        this.setData({
            query: a.detail
        });
    },
    selectFromLanguage: function(a) {
        var e = this;
        wx.showActionSheet({
            itemList: e.data.languageList,
            success: function(a) {
                var t = e.data.language[a.tapIndex];
                e.setData({
                    fromLanguage: t
                }), 
                wx.setStorage({
                    key: "fromLanguage",
                    data: t
                });
            }
        });
    },
    selectToLanguage: function(a) {
        var e = this;
        wx.showActionSheet({
            itemList: e.data.languageList,
            success: function(a) {
                var t = e.data.language[a.tapIndex];
                e.setData({
                    toLanguage: t
                }),
                 wx.setStorage({
                    key: "toLanguage",
                    data: t
                });
            }
        });
    },
    translate: function() {
        var t = this, n = t.data.query, o = t.data.fromLanguage.code, g = t.data.toLanguage.code;
        console.log(n + "---" + o + "---" + g), "" != t.data.query && null != t.data.query ? app.testText(n, function(e) {
            87014 != e.data.errcode && wx.request({
                url: url + "/tusi/trans.php",
                method: "POST",
                data: {
                    from: t.data.fromLanguage.code,
                    to: t.data.toLanguage.code,
                    query: t.data.query
                },
                header: {
                    "content-type": "application/x-www-form-urlencoded"
                },
                success: function(a) {
                    a.data.answer && (t.setData({
                        result: a.data.answer
                    }), t.setData({
                        voiceUrl: a.data.voic
                    }));
                },
                fail: function(a) {
                    console.log(a), wx.showToast({
                        title: "翻译失败，待会再试试吧",
                        icon: "none"
                    });
                }
            });
        }) : wx.showToast({
            title: "请先输入翻译原文",
            icon: "none"
        });
    },
    getVoice: function() {
        var a = wx.createInnerAudioContext();
        a.src = this.data.voiceUrl, a.play();
    },
    onShareAppMessage:shareUtil.shareConfig,
    onShareTimeline:shareUtil.shareConfig,
});